/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.nii.qlks.GUI;

import edu.nii.qlks.ControlLayer.PhieuThuControl;
import edu.nii.qlks.ControlLayer.KhachHangControl;
import edu.nii.qlks.ControlLayer.LoaiKhachHangControl;
import edu.nii.qlks.ControlLayer.PhieuDenControl;
import edu.nii.qlks.ControlLayer.PhongControl;
import edu.nii.qlks.ControlLayer.PhuThuControl;
import edu.nii.qlks.ControlLayer.ThamSoControl;
import edu.nii.qlks.DataTransferObject.PhieuThuInfo;
import edu.nii.qlks.DataTransferObject.PhieuDenInfo;
import edu.nii.qlks.DataTransferObject.PhuThuInfo;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import org.jdesktop.swingx.autocomplete.AutoCompleteDecorator;

/**
 *
 * @author MARS_NHO
 */
public class jfrm_LapHoaDonThanhToan extends javax.swing.JInternalFrame {

    PhieuThuControl hoaDonCT;
    PhieuThuInfo hoaDonInfo;
    KhachHangControl khachHangCT;
    PhongControl phongCT;
    PhieuDenControl phieuThuePhongCT;
    PhieuDenInfo phieuThuePhongInfo;
    PhuThuControl phuThuCT;
    PhuThuInfo phuThuInfo;
    ThamSoControl thamSoCT;
    LoaiKhachHangControl loaiKhachHangCT;
    
    /**
     * Creates new form jfrm_LapHoaDonThanhToan
     */
    public jfrm_LapHoaDonThanhToan(){
        initComponents();
        
        hoaDonCT = new PhieuThuControl();
        hoaDonInfo = new PhieuThuInfo();
        khachHangCT = new KhachHangControl();
        phongCT = new PhongControl();
        phieuThuePhongCT = new PhieuDenControl();
        phieuThuePhongInfo = new PhieuDenInfo();
        phuThuCT = new PhuThuControl();
        phuThuInfo = new PhuThuInfo();
        loaiKhachHangCT = new LoaiKhachHangControl();
        thamSoCT = new ThamSoControl();
        
        Load_Table();
        Test_Date();
        try {
            Load_Form();
            Load_Phong_In_Table();
        } catch (Exception ex) {
            Logger.getLogger(jfrm_LapHoaDonThanhToan.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
    public void Test_Date(){
        //RANG BUOC NGAY THANG THUE PHONG
        jXDatePicker_NgayThanhToan.setFormats("dd/MM/yyyy");
        jXDatePicker_NgayThanhToan.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Calendar date = Calendar.getInstance();
                if (jXDatePicker_NgayThanhToan.getDate().compareTo(date.getTime()) > 0){
                    JOptionPane.showMessageDialog(rootPane, "Không chọn ngày lập sau ngày hiện hành!");
                    jXDatePicker_NgayThanhToan.setDate(new Date());
                }
                //MOC NGAY LAP: 1/5/2013
                date.set(Calendar.YEAR, 2013);
                date.set(Calendar.MONTH, 4);
                date.set(Calendar.DAY_OF_MONTH, 1);
                if (jXDatePicker_NgayThanhToan.getDate().compareTo(date.getTime()) < 0){
                    JOptionPane.showMessageDialog(rootPane, "Không chọn ngày lập trước ngày: " + date.getTime());
                    jXDatePicker_NgayThanhToan.setDate(new Date());
                }
            }
        });
    }
    
    public void Load_Form() throws Exception{
        Vector vt = new Vector();
        vt = khachHangCT.LayDS(); 
        for (int i = 0; i < vt.size(); i++){
            Vector vt_row = new Vector();
            vt_row = (Vector) vt.get(i);
            String str = vt_row.get(1).toString().trim();
            jComboBox_TenKhachHang.addItem(str);
        }
        //TAO TINH NANG NHAP TU DONG TRONG COMBOBOX_TENPHONG
        AutoCompleteDecorator.decorate(jComboBox_TenKhachHang);
        
        
        
    }
    
    public void Load_Table(){
        Object[] add = {"1", null, null, null, null};
        ((DefaultTableModel) jXTable_Phong.getModel()).addRow(add);
    }
    
    public void Load_Phong_In_Table() throws Exception{
        jXTable_Phong.getColumnModel().getColumn(0).setPreferredWidth(5);
        jXTable_Phong.getColumnModel().getColumn(1).setPreferredWidth(150);
        jXTable_Phong.getColumnModel().getColumn(2).setPreferredWidth(50);
        jXTable_Phong.getColumnModel().getColumn(3).setPreferredWidth(50);
        jXTable_Phong.getColumnModel().getColumn(4).setPreferredWidth(100);
        
        //TAO COMBOBOX VAO TRUONG TEN PHONG TRONG BANG
        Vector com_value = new Vector();
        JComboBox com = new JComboBox();
         com_value = phongCT.LayDS("TinhTrangPhong = 1"); //LAY DS PHONG DA THUE, TT= 1
        for (int i = 0; i < com_value.size(); i++){
            Vector vt_row = new Vector();
            vt_row = (Vector) com_value.get(i);
            
            String str = vt_row.get(1).toString();
            com.addItem(str);
        }
        //TAO TINH NANG NHAP TU DONG TRONG COMBOBOX_LOAI KHACH HANG
        AutoCompleteDecorator.decorate(com);
        
        //ADD COMBOBOX VAO JXTABLE
        DefaultCellEditor dce = new DefaultCellEditor(com);
        jXTable_Phong.getColumnModel().getColumn(1).setCellEditor(dce);
    }
    
    public void Get_HoaDonInfo(){
        String str = "";
        Vector vt_ds = new Vector();
        try {
            vt_ds = hoaDonCT.LayDS();
            if (vt_ds.size() > 0){
                Vector vt_row = new Vector();
                vt_row = (Vector) vt_ds.get(vt_ds.size() - 1);
                str = vt_row.get(0).toString().trim(); //ma hoa don              
                String mahd = hoaDonCT.TaoMaTuDong(str, 3);
                hoaDonInfo.setMaHoaDon(mahd);
            }
            else{
                String mahd = hoaDonCT.TaoMaTuDong("MHD000", 3);
                hoaDonInfo.setMaHoaDon(mahd);
            }
            System.out.println("ma hoa don: " + hoaDonInfo.getMaHoaDon());
            
            String tenKH = jComboBox_TenKhachHang.getSelectedItem().toString().trim();
            Vector vt_KH = khachHangCT.LayDS("TenKhachHang = N'" + tenKH + "'");//phongCT.LayDonGiaPhong(tenPhong.trim());
            Vector vt_row_KH = (Vector)vt_KH.get(0);
            
            hoaDonInfo.setMaKhachHang(vt_row_KH.get(0).toString().trim());
            System.out.println("ma khach hang: " + hoaDonInfo.getMaKhachHang());
            
            java.sql.Date insertDate = new java.sql.Date(jXDatePicker_NgayThanhToan.getDate().getTime());
            hoaDonInfo.setNgayThanhToan(insertDate);
            System.out.println("ngay thanh toan: " + hoaDonInfo.getNgayThanhToan());
            
            hoaDonInfo.setTriGia(0);
            System.out.println("tri gia: " + hoaDonInfo.getTriGia());
            
        } catch (Exception ex) {
            Logger.getLogger(LapDanhMucPhong.class.getName()).log(Level.SEVERE, null, ex);
            return;
        }
    }
    
    public int Get_Count_Phong(){
        int rows_data = 0;
        for (int i = 0; i < jXTable_Phong.getRowCount(); i++){
            boolean isData = false;
            for (int j = 1; j < jXTable_Phong.getColumnCount(); j++) {
                //j = 0: STT, ta khong xet
                try {
                    String temp = (String) jXTable_Phong.getValueAt(i, j);
                    System.out.println("....." + temp);
                    if (!temp.trim().equals("")) {
                        isData = true;
                    }
                } catch (Exception e) {
                }
            }
            if (isData){
                rows_data++;
                System.out.println("----------"+rows_data);
            }
        }
        return rows_data;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jXPanel_HoaDon = new org.jdesktop.swingx.JXPanel();
        jXLabel_TenKhachHang = new org.jdesktop.swingx.JXLabel();
        jXLabel_CMND = new org.jdesktop.swingx.JXLabel();
        jXLabel_NgayThanhToan = new org.jdesktop.swingx.JXLabel();
        jComboBox_TenKhachHang = new javax.swing.JComboBox();
        jXTextField_CMND = new org.jdesktop.swingx.JXTextField();
        jXDatePicker_NgayThanhToan = new org.jdesktop.swingx.JXDatePicker();
        jXLabel_DiaChi = new org.jdesktop.swingx.JXLabel();
        jXTextField_DiaChi = new org.jdesktop.swingx.JXTextField();
        jXLabel_TriGia = new org.jdesktop.swingx.JXLabel();
        jXTextField_TriGia = new org.jdesktop.swingx.JXTextField();
        jXPanel_Phong = new org.jdesktop.swingx.JXPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jXTable_Phong = new org.jdesktop.swingx.JXTable();
        jXButton_LamMoi = new org.jdesktop.swingx.JXButton();
        jXButton_LapPhieu = new org.jdesktop.swingx.JXButton();
        jXButton_Thoat = new org.jdesktop.swingx.JXButton();
        jXLabel_TieuDe = new org.jdesktop.swingx.JXLabel();

        setTitle("Lập hóa đơn");
        setToolTipText("Lập hóa đơn");

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        jXPanel_HoaDon.setBackground(new java.awt.Color(255, 255, 255));
        jXPanel_HoaDon.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Thông tin hóa đơn", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 12))); // NOI18N
        jXPanel_HoaDon.setToolTipText("Nhập thông tin của hóa đơn");

        jXLabel_TenKhachHang.setBackground(new java.awt.Color(255, 255, 255));
        jXLabel_TenKhachHang.setText("Tên khách hàng");
        jXLabel_TenKhachHang.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jXLabel_CMND.setBackground(new java.awt.Color(255, 255, 255));
        jXLabel_CMND.setText("CMND");
        jXLabel_CMND.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jXLabel_NgayThanhToan.setBackground(new java.awt.Color(255, 255, 255));
        jXLabel_NgayThanhToan.setText("Ngày thanh toán");
        jXLabel_NgayThanhToan.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jComboBox_TenKhachHang.setToolTipText("Chọn tên khách hàng thanh toán");
        jComboBox_TenKhachHang.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox_TenKhachHangActionPerformed(evt);
            }
        });

        jXTextField_CMND.setEditable(false);
        jXTextField_CMND.setBackground(new java.awt.Color(255, 255, 255));
        jXTextField_CMND.setToolTipText("CMND khách hàng");

        jXDatePicker_NgayThanhToan.setToolTipText("Chọn ngày thanh toán");

        jXLabel_DiaChi.setBackground(new java.awt.Color(255, 255, 255));
        jXLabel_DiaChi.setText("Địa Chỉ");
        jXLabel_DiaChi.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jXTextField_DiaChi.setEditable(false);
        jXTextField_DiaChi.setBackground(new java.awt.Color(255, 255, 255));
        jXTextField_DiaChi.setToolTipText("Địa chỉ khách hàng");

        jXLabel_TriGia.setBackground(new java.awt.Color(255, 255, 255));
        jXLabel_TriGia.setText("Trị giá");
        jXLabel_TriGia.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jXTextField_TriGia.setEditable(false);
        jXTextField_TriGia.setBackground(new java.awt.Color(255, 255, 255));
        jXTextField_TriGia.setToolTipText("Trị giá của hóa đơn");

        javax.swing.GroupLayout jXPanel_HoaDonLayout = new javax.swing.GroupLayout(jXPanel_HoaDon);
        jXPanel_HoaDon.setLayout(jXPanel_HoaDonLayout);
        jXPanel_HoaDonLayout.setHorizontalGroup(
            jXPanel_HoaDonLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jXPanel_HoaDonLayout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addGroup(jXPanel_HoaDonLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jXLabel_TenKhachHang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jXLabel_CMND, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jXLabel_DiaChi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(22, 22, 22)
                .addGroup(jXPanel_HoaDonLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jXPanel_HoaDonLayout.createSequentialGroup()
                        .addComponent(jXTextField_DiaChi, javax.swing.GroupLayout.PREFERRED_SIZE, 199, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jXPanel_HoaDonLayout.createSequentialGroup()
                        .addGroup(jXPanel_HoaDonLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jComboBox_TenKhachHang, 0, 160, Short.MAX_VALUE)
                            .addComponent(jXTextField_CMND, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 89, Short.MAX_VALUE)
                        .addGroup(jXPanel_HoaDonLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jXLabel_NgayThanhToan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jXLabel_TriGia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(29, 29, 29)
                        .addGroup(jXPanel_HoaDonLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jXDatePicker_NgayThanhToan, javax.swing.GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE)
                            .addComponent(jXTextField_TriGia, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(42, 42, 42))))
        );
        jXPanel_HoaDonLayout.setVerticalGroup(
            jXPanel_HoaDonLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jXPanel_HoaDonLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jXPanel_HoaDonLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jXLabel_TenKhachHang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jXLabel_NgayThanhToan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBox_TenKhachHang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jXDatePicker_NgayThanhToan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(21, 21, 21)
                .addGroup(jXPanel_HoaDonLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jXPanel_HoaDonLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jXLabel_TriGia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jXTextField_TriGia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jXPanel_HoaDonLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jXLabel_CMND, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jXTextField_CMND, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGroup(jXPanel_HoaDonLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jXPanel_HoaDonLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 27, Short.MAX_VALUE)
                        .addComponent(jXLabel_DiaChi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(jXPanel_HoaDonLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jXTextField_DiaChi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
        );

        jXPanel_Phong.setBackground(new java.awt.Color(255, 255, 255));
        jXPanel_Phong.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Chi tiết hóa đơn", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 12))); // NOI18N
        jXPanel_Phong.setToolTipText("Danh sách phòng đã thuê");
        jXPanel_Phong.setLayout(new java.awt.BorderLayout());

        jXTable_Phong.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "STT", "Tên phòng", "Số ngày thuê", "Đơn giá", "Thành tiền"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jXTable_Phong.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jXTable_PhongKeyReleased(evt);
            }
        });
        jScrollPane1.setViewportView(jXTable_Phong);

        jXPanel_Phong.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jXButton_LamMoi.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/refresh.png"))); // NOI18N
        jXButton_LamMoi.setText("Làm mới");
        jXButton_LamMoi.setToolTipText("Lập thêm hóa đơn thanh toán");
        jXButton_LamMoi.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jXButton_LamMoi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jXButton_LamMoiActionPerformed(evt);
            }
        });

        jXButton_LapPhieu.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/add.png"))); // NOI18N
        jXButton_LapPhieu.setText("Lập phiếu");
        jXButton_LapPhieu.setToolTipText("Lập hóa đơn thanh toán phòng");
        jXButton_LapPhieu.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jXButton_LapPhieu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jXButton_LapPhieuActionPerformed(evt);
            }
        });

        jXButton_Thoat.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/exit_16.png"))); // NOI18N
        jXButton_Thoat.setText("Thoát");
        jXButton_Thoat.setToolTipText("Thoát khỏi");
        jXButton_Thoat.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jXButton_Thoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jXButton_ThoatActionPerformed(evt);
            }
        });

        jXLabel_TieuDe.setText("LẬP HÓA ĐƠN THANH TOÁN");
        jXLabel_TieuDe.setToolTipText("");
        jXLabel_TieuDe.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(268, 268, 268)
                .addComponent(jXLabel_TieuDe, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(97, 97, 97)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jXButton_LamMoi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jXButton_LapPhieu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jXButton_Thoat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jXPanel_HoaDon, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jXPanel_Phong, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap(113, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jXLabel_TieuDe, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jXPanel_HoaDon, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(37, 37, 37)
                .addComponent(jXPanel_Phong, javax.swing.GroupLayout.PREFERRED_SIZE, 208, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jXButton_Thoat, javax.swing.GroupLayout.DEFAULT_SIZE, 37, Short.MAX_VALUE)
                    .addComponent(jXButton_LapPhieu, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jXButton_LamMoi, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jComboBox_TenKhachHangActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox_TenKhachHangActionPerformed

        if (jComboBox_TenKhachHang.getItemCount() > 0){
            try {
                String str = jComboBox_TenKhachHang.getSelectedItem().toString().trim();
                Vector vt = khachHangCT.LayDS("TenKhachHang = N'" + str + "'");
                Vector vt_row = (Vector)vt.get(0);
                jXTextField_CMND.setText(vt_row.get(3).toString().trim());
                jXTextField_DiaChi.setText(vt_row.get(4).toString().trim());
            } catch (Exception ex) {
                //Logger.getLogger(jfrm_LapPhieuThuePhong.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }//GEN-LAST:event_jComboBox_TenKhachHangActionPerformed

    private void jXTable_PhongKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jXTable_PhongKeyReleased

        if (evt.getKeyCode() == evt.VK_DOWN) {
            int row = jXTable_Phong.getSelectedRow();
            int a = jXTable_Phong.getRowCount() - 1;
            Object[] add = {null, null, null, null, null};
            if (row == a) {
                boolean isData = false;
                for (int j = 1; j < jXTable_Phong.getColumnCount(); j++) {
                    //j = 0: STT, ta khong xet
                    try {
                        String temp = (String) jXTable_Phong.getValueAt(row, j);
                        System.out.println("-----" + temp);
                        if (!temp.trim().equals("")) {
                            isData = true;
                        }
                    } catch (Exception ex) {
                    }
                }
                System.out.println("Them 1 dong moi");
                if (isData == true) {
                    ((DefaultTableModel) jXTable_Phong.getModel()).addRow(add);
                    String str_row = String.valueOf(row+1+1);
                    jXTable_Phong.setValueAt(str_row, row+1, 0);
                }

            }
        }

    }//GEN-LAST:event_jXTable_PhongKeyReleased

    private void jXButton_LamMoiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jXButton_LamMoiActionPerformed
        jComboBox_TenKhachHang.setEditable(true);
        jComboBox_TenKhachHang.removeAllItems();
        jXTextField_TriGia.setText("");
        jXButton_LapPhieu.setEnabled(true);
        jXDatePicker_NgayThanhToan.setEditable(true);
        jXTable_Phong.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"1", null, null, null, null}
            },
            new String [] {
                "STT", "Tên phòng", "Số ngày thuê", "Đơn giá", "Thành tiền"
            }
        ));
        try {
            Load_Form();
            Load_Phong_In_Table();
        } catch (Exception ex) {
            Logger.getLogger(jfrm_LapPhieuThuePhong.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_jXButton_LamMoiActionPerformed

    private void jXButton_LapPhieuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jXButton_LapPhieuActionPerformed
        if (jComboBox_TenKhachHang.getSelectedItem().toString().trim().equals("")){
            JOptionPane.showMessageDialog(rootPane, "Vui lòng chọn tên phòng!");
            jComboBox_TenKhachHang.requestFocus();
            return;
        }
        if (jXDatePicker_NgayThanhToan.getDate() == null){
            JOptionPane.showMessageDialog(rootPane, "Vui lòng chọn ngày lập phiếu!");
            jXDatePicker_NgayThanhToan.requestFocus();
            return;
        }

        Get_HoaDonInfo();

        //-----------UPDATE LAI PHIEU THUE PHONG----------------//
        int rows_data = 0;
        rows_data = Get_Count_Phong();

        if (rows_data != 0){
            try {
                Vector vt = new Vector();
                for (int i = 0; i < rows_data; i++){
                    float phuThu = 0;
                    float heSoLoaiKhach = 1;
                    
                    String field = "";
                    field = jXTable_Phong.getValueAt(i, 1).toString().trim();
                    System.out.println("ten phong: "+field);
                    Vector vt_PT = new Vector();
                    vt_PT = phieuThuePhongCT.LayDS_ChuaTT(field);
                    
                    if (vt_PT.size() > 0){
                        Vector vt_row = (Vector)vt_PT.get(vt_PT.size() - 1);
                        field = vt_row.get(0).toString().trim();
                        phieuThuePhongInfo.setMaPhieuThue(field);
                        System.out.println("ma phieu thue :" + field);
                        
                        field = vt_row.get(1).toString().trim();
                        phieuThuePhongInfo.setMaPhong(field);
                        System.out.println("ma phong :" + field);
                        
                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                        String date = vt_row.get(2).toString().trim();
                        Date insertDate = format.parse(date);
                        phieuThuePhongInfo.setNgayThuePhong(insertDate);
                        
                        Calendar cal = Calendar.getInstance();
                        cal.setTime(phieuThuePhongInfo.getNgayThuePhong());
                        int ngaythue = cal.get(Calendar.DATE);
                        System.out.println("ngay thue : " + ngaythue);
                        cal.setTime(hoaDonInfo.getNgayThanhToan());
                        int ngaythanhtoan = cal.get(Calendar.DATE);
                        System.out.println("ngay thanh toan : " + ngaythanhtoan);
                        int soNgayThue = ngaythanhtoan - ngaythue;
                        System.out.println("so ngay thue : " + soNgayThue);
                        
                        field = vt_row.get(3).toString().trim();
                        float dongiathue = Float.parseFloat(field);
                        phieuThuePhongInfo.setDonGiaThue(dongiathue);
                        System.out.println("don gia thue: " + phieuThuePhongInfo.getDonGiaThue());
                        
                        java.sql.Date ngaythuephong = new java.sql.Date(phieuThuePhongInfo.getNgayThuePhong().getTime());
                        phieuThuePhongInfo.setNgayThuePhong(ngaythuephong);
                        System.out.println("ngay thue phong: " + phieuThuePhongInfo.getNgayThuePhong());
                        
                        phieuThuePhongInfo.setSoNgayThue(soNgayThue);
                        System.out.println("so ngay thue: " + phieuThuePhongInfo.getSoNgayThue());
                        phieuThuePhongInfo.setThanhTien(phieuThuePhongInfo.getDonGiaThue() * phieuThuePhongInfo.getSoNgayThue());
                        System.out.println("thanh tien: " + phieuThuePhongInfo.getThanhTien());
                        phieuThuePhongInfo.setMaHoaDon(hoaDonInfo.getMaHoaDon());
                        System.out.println("ma hoa don: " + phieuThuePhongInfo.getMaHoaDon());
                    }
                    
                    int soKhachTheoDonGia = 0;
                    vt = thamSoCT.LayDS();
                    if (vt.size() > 0){
                        Vector vt_row = (Vector)vt.get(0);
                        field = vt_row.get(1).toString().trim();
                        soKhachTheoDonGia = Integer.parseInt(field);
                        System.out.println("so khach theo don gia: " + soKhachTheoDonGia);
                    }
                    
                    
                    int soLuongKhach = 0;
                    vt = phieuThuePhongCT.LaySoLuongKhach(phieuThuePhongInfo.getMaPhieuThue());
                    if (vt.size() > 0){
                        Vector vt_row = (Vector)vt.get(0);
                        field = vt_row.get(0).toString().trim();
                        soLuongKhach = Integer.parseInt(field);
                        System.out.println("so luong khach: "+soLuongKhach);
                    }
                    
                    
                    if (soLuongKhach <= soKhachTheoDonGia){
                        phuThu = 0;
                    }
                    else{
                        int thuTuKhachTinhPhu = 0;
                        vt = phuThuCT.LayDS();
                        if (vt.size() > 0){
                            for (int j = 0; j < vt.size(); j++){
                                Vector vt_row = (Vector)vt.get(j);
                                field = vt_row.get(1).toString().trim();
                                thuTuKhachTinhPhu = Integer.parseInt(field);
                                if (soLuongKhach == thuTuKhachTinhPhu){
                                    field = vt_row.get(2).toString().trim();
                                    phuThu = Float.parseFloat(field);
                                    System.out.println("phu thu: "+phuThu);
                                }
                            }
                        }
                    }
                    
                    //KIEM TRA CO KHACH NUOC NGOAI TRONG PHIEU THUE K?
                    vt = phieuThuePhongCT.LayLoaiKhach(phieuThuePhongInfo.getMaPhieuThue());
                    if (vt.size() > 0){
                        //int slKhach = vt.size();
                        for (int j = 0; j < vt.size(); j++){
                            Vector vt_row = (Vector)vt.get(j);
                            String maloaikhach1 = vt_row.get(0).toString().trim();
                            Vector _vt = loaiKhachHangCT.LayDS("TenLoaiKhach = N'Nước Ngoài'");
                            if (_vt.size() > 0){
                                Vector _vt_row = (Vector)_vt.get(0);
                                String maloaikhach2 = _vt_row.get(0).toString().trim();
                                
                                if (maloaikhach1.equals(maloaikhach2)){
                                    field = _vt_row.get(2).toString().trim();
                                    heSoLoaiKhach = Float.parseFloat(field);
                                    System.out.println("he so loai khach: " + heSoLoaiKhach);
                                }
                            }
                        }
                    }
                    
                    float thanhTien = phieuThuePhongInfo.getSoNgayThue() * (1 + phuThu) * phieuThuePhongInfo.getDonGiaThue() * heSoLoaiKhach;
                    phieuThuePhongInfo.setThanhTien(thanhTien);
                    System.out.println("thanh tien: " + thanhTien);
                    
                    //HIEN THI THONG TIN PHIEU THUE PHONG LEN TABLE
                    jXTable_Phong.setValueAt(phieuThuePhongInfo.getSoNgayThue(), i, 2);
                    jXTable_Phong.setValueAt(phieuThuePhongInfo.getDonGiaThue(), i, 3);
                    jXTable_Phong.setValueAt(phieuThuePhongInfo.getThanhTien(), i, 4);
                    //jXTable_Phong.updateUI();
                    
                    //LAY TRI GIA HOA DON
                    hoaDonInfo.setTriGia(hoaDonInfo.getTriGia() + phieuThuePhongInfo.getThanhTien());
                    
                    //UPDATE PHIEU THUE PHONG
                    phieuThuePhongCT.Update(phieuThuePhongInfo);
                    
                    //UPDATE TINH TRANG PHONG = CHUA THUE
                    phongCT.Update_TinhTrangPhong("'" + phieuThuePhongInfo.getMaPhong() + "'", 0);
                }
                String trigia = String.valueOf(hoaDonInfo.getTriGia());
                jXTextField_TriGia.setText(trigia);
                hoaDonCT.Insert(hoaDonInfo);
                System.out.println(" them hoa don thanh cong!");
                JOptionPane.showMessageDialog(rootPane, "Thêm hóa đơn thành công!");
                
                //SET CAC COMPONENT TREN FORM
                jXButton_LapPhieu.setEnabled(false);
                jComboBox_TenKhachHang.setEditable(false);
                jXDatePicker_NgayThanhToan.setEditable(false);

            } catch (Exception ex) {
                Logger.getLogger(jfrm_LapPhieuThuePhong.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
        else{
            JOptionPane.showMessageDialog(rootPane, "Vui lòng chọn phòng cần thanh toán!");
            return;
        }
    }//GEN-LAST:event_jXButton_LapPhieuActionPerformed

    private void jXButton_ThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jXButton_ThoatActionPerformed
        this.dispose();
    }//GEN-LAST:event_jXButton_ThoatActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox jComboBox_TenKhachHang;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private org.jdesktop.swingx.JXButton jXButton_LamMoi;
    private org.jdesktop.swingx.JXButton jXButton_LapPhieu;
    private org.jdesktop.swingx.JXButton jXButton_Thoat;
    private org.jdesktop.swingx.JXDatePicker jXDatePicker_NgayThanhToan;
    private org.jdesktop.swingx.JXLabel jXLabel_CMND;
    private org.jdesktop.swingx.JXLabel jXLabel_DiaChi;
    private org.jdesktop.swingx.JXLabel jXLabel_NgayThanhToan;
    private org.jdesktop.swingx.JXLabel jXLabel_TenKhachHang;
    private org.jdesktop.swingx.JXLabel jXLabel_TieuDe;
    private org.jdesktop.swingx.JXLabel jXLabel_TriGia;
    private org.jdesktop.swingx.JXPanel jXPanel_HoaDon;
    private org.jdesktop.swingx.JXPanel jXPanel_Phong;
    private org.jdesktop.swingx.JXTable jXTable_Phong;
    private org.jdesktop.swingx.JXTextField jXTextField_CMND;
    private org.jdesktop.swingx.JXTextField jXTextField_DiaChi;
    private org.jdesktop.swingx.JXTextField jXTextField_TriGia;
    // End of variables declaration//GEN-END:variables
}
